#include<stdio.h>
#include<malloc.h>
int *a;
int tl;
//////////////////////////////////////////////////////////////

//int *a=(int *)malloc(1000*sizeof(int));
////////////////////////////////////////////////////////////

void insert1()
{ int n,i,j,tmp;
printf("enter the no. you want to insert");
scanf("%d",&n);
printf("enter the index where u want to insert this no.");
scanf("%d",&i);

tl=tl+1;

for(j=tl;j>i;j--)
{a[j]=a[j-1];
}
a[i]=n;

printf(" the list after inserting %d  at %d postion is \n\n",n,i);

for(j=0;j<tl;j++)
{
printf("%d -> ",a[j]);
}
}

///////////////////////////////////////////////////////////////

void count1()
{ int n,i;
int count=0;
printf("enter the element whose no. of occurences u want ");
scanf("%d",&n);

for(i=0;i<tl;i++)
{if(a[i]==n)
{count++;
}
}
printf("the no. of times  %d  ", n);
printf("is occuring is %d  ",count);
}


void remove1()
{ int rn,k,f=0,i;
printf(" enter the no to be deleted  ");
scanf("%d",&rn);

for(i=0; i<tl;i++)
{ if(a[i]==rn)
{k=i;
f++;
break;}
}

if(f==0)
{ printf(" element not found ");}
else 
{
for(i=k;i<tl;i++)
{ a[i]=a[i+1];}
tl=tl-1;
}
printf("\n\nthe list after deleting that element is  \n\n  ");
for(i=0;i<tl;i++)
{
printf("  %d -> ",a[i]);
}

}
/////////////////////////////////////////////////////////////////

void reverse()
{ int i,tmp;
int k=tl/2;
for(i=0; i<k; i++)
       {
               tmp=a[i];
               a[i]=a[tl-1-i];
               a[tl-1-i]=tmp;
       }

printf("\n\nthe reversed list is    ");
for(i=0;i<tl;i++)
{
printf("  %d -> ",a[i]);
}

}
/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
void pop1()
{ int n,i;
printf("enter the index from where u want to delete the element");
scanf("%d",&n);
//a[n]=0;
for(i=n;i<tl;i++)
{a[i]=a[i+1];
}
tl=tl-1;
printf("\n\nthe new list is    ");
for(i=0;i<tl;i++)
{
printf("  %d  ->",a[i]);
}
}
/////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////

void index1()
{int i,flag=0,k,in;
printf("enter the element whose index u want");
scanf("%d",&in);

for(i=0;i<tl;i++)
{
if(a[i]==in)
break;
else
flag++;
}
k=i+1;
if(flag==1)
{ printf("element not found");}
else
printf("the element is found to be at %d  position",k);

}
///////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////

void extend()
{ int ell=0;
char c='k';
int *b;
//int total;
printf(" enter the new list which is to be extended\n\n");
printf("start entering nos.separated by comma  (end wid a dot)\n\n");
while(c!='.')
{
b=(int*)realloc(b,(ell+1)*sizeof(int*));
scanf("%d",&b[ell]);
scanf("%c",&c);
ell++;
};
int i;
printf("\n\nthe new list is    ");
for(i=0;i<ell;i++)
{
printf("  %d -> ",b[i]);
}
a=(int*)realloc(a,(tl+ell)*sizeof(int*));

for(i=0;i<ell;i++)
{a[tl+i]=b[i];
}

tl=tl+ell;

printf(" \n\n the extended list is\n  ");
for(i=0;i<tl;i++)
{ printf("%d -> ",a[i]);}
}
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////
void append()
{int num;
int i;
 printf("enter the element you want to append  ");
scanf("%d",&a[tl]);
tl=tl+1;
printf(" the new list is  ");
	for(i=0;i<tl;i++)
	{ printf("%d -> ", a[i]);
	}
}

/////////////////////////////////////////////////////////////
void insertlist()
{//a=(int *)malloc(1000*sizeof(int));
//int i=0;
char c='k';
//int total;
printf("\n\n start entering nos.separated by comma  (end wid a dot) \n");
while(c!='.')
{
a=(int*)realloc(a,(tl+1)*sizeof(int*));
scanf("%d",&a[tl]);

//c=getchar();
//printf("Enter 0/1 ");
scanf("%c",&c);
tl++;
};
int i;
printf("\n\nthe list is    ");
for(i=0;i<tl;i++)
{
printf("  %d ->",a[i]);
}
}

////////////////////////////////

void sort()
{ 	int i,j,tmp;
	 
printf("\n\n\n\n tl = %d \n\n\n\n",tl);
	/*for(i=0;i<tl;i++)
	{
		min = i;
		 for(j=0;j<tl; j++)
		{ if(a[j]<a[min])
			{ 
				min = j;
			}
		}
		tmp = a[i];
		a[i] = a[min];
		a[min] = tmp;
	}*/

for(i=0;i<tl-1;i++)
{
for(j=0;j<tl-1;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}}}

	printf("the sorted list is ");
	for(i=0;i<tl;i++)
	{ printf("%d -> ", a[i]);
	}
}




void main()
{ char choice='y';

int ch;


while(choice=='y')
{
printf("\n\n\t\tWELCOME TO THE LIST WORLD \n");
printf("\n\t\tENJOY THE FOLLOWING OPTIONS \n\n");
printf("\t\t1.MAKE A LIST \n");
printf("\t\t2.SORTING \n");
printf("\t\t3.APPEND A NEW NUMBER \n");
printf("\t\t4.EXTEND THE LIST\n");
printf("\t\t5.INDEX OF AN ELEMENT \n");
printf("\t\t6.POP A NO. \n");
printf("\t\t7.REVERSE THE LIST \n");
printf("\t\t8.REMOVE AN ELEMENT \n");
printf("\t\t9.COUNT\n");
printf("\t\t10.INSERT A NO. AT GIVEN POSTION\n\n\n");

printf("ENTER CHOICE YOU WANT TO GO FOR  ");
scanf("%d",&ch);

if(ch>0 && ch<11)
{
if(ch==1)
{ insertlist(); } //to make a new list

else if(ch==2)
{sort();} // to sort the entered list

else if(ch==3)
{append();} // to insert a new element in the list

else if(ch==4)
{extend();} // to insert a new list in the list

else if(ch==5)
{index1();} // to get an index of an element in the list

else if(ch==6)
{pop1();} // to remove the element at a particular index in the list

else if(ch==7)
{reverse();} // to reverse the list

else if(ch==8)
{remove1();} // to remove the first occurence of the particular element

else if(ch==9)
{count1();} // to count the occurences of a particular element

else if(ch==10)
{insert1();} // to insert a particular no. at particular position

}

else
{printf("WRONG OPTION ENTERED!!!!!!\n\n ");
exit(0);
}

/*
else if(ch==4)
{
int nu;
printf("enter the no. to be deleted");
scanf("%d",&nu);
pop(start,nu);} //  to delete an element from the list

else if(ch==5)
{printf("enter the no. to be searched : ");

scanf("%d",&item);

index(item);
} // to print the index of a particular no.

else if(ch==6)
{count();} // to count the  total no. of elements
/*
else if(ch==7)
{reverse();} // to reverse the list
*/


fflush(stdin);
printf("\n\n\n\n DO YOU WANT TO DO MORE FUNCTIONS (y/n)  \n\n ");
scanf("%s",&choice);
}
}

